package cn.stu.projectsummit.blog.service.impl;

import cn.stu.projectsummit.blog.mapper.BlogMapper;
import cn.stu.projectsummit.blog.pojo.dto.BlogQuery;
import cn.stu.projectsummit.blog.pojo.dto.BlogSaveParam;
import cn.stu.projectsummit.blog.pojo.entity.Blog;
import cn.stu.projectsummit.blog.pojo.vo.BlogVO;
import cn.stu.projectsummit.blog.service.BlogService;
import cn.stu.projectsummit.likes.pojo.entity.Likes;
import cn.stu.projectsummit.likes.service.LikesService;
import cn.stu.projectsummit.user.mapper.UserMapper;
import cn.stu.projectsummit.user.pojo.dto.UserQuery;
import cn.stu.projectsummit.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    LikesService likesService;
    @Override
    public List<BlogVO> selectBlog(BlogQuery blogQuery) {
        log.debug("查询帖子信息业务 blogQuery={}",blogQuery);
        if (blogQuery.getUsername() !=null && !blogQuery.getUsername().equals("")){
            UserVO userVO = userMapper.selectByUserName(blogQuery.getUsername());
            blogQuery.setUserId(userVO.getId());
        }
        List<BlogVO> listBlogVO = blogMapper.selectBlog(blogQuery);
        log.debug("listBlogVO={}",listBlogVO);
        for (int i =0;i<listBlogVO.size();i++){
            BlogVO blogVO = listBlogVO.get(i);
            setUserName(blogVO);
        }
        log.debug("查询点赞帖子");
        List<Likes> likesList = likesService.select(blogQuery.getLikeUserId());
        List<Long> likeBlogIdList = likesList.stream().map(Likes::getBlogId).collect(Collectors.toList());

        for(BlogVO blogVO:listBlogVO) {
            if (likeBlogIdList.contains(blogVO.getId())){
                blogVO.setBeLikes(true);
            } else {
                blogVO.setBeLikes(false);
            }
        }
        List<Long> likeCountBlogIdList = listBlogVO.stream().map(BlogVO::getId).collect(Collectors.toList());
        List<Likes> likeCountList = likesService.selectByBlogIdList(likeCountBlogIdList);
        Map<Long, List<Likes>> likeCountMap = likeCountList.stream().collect(Collectors.groupingBy(Likes::getBlogId));
        for(BlogVO blogVO:listBlogVO) {
            if (likeCountMap.containsKey(blogVO.getId())){
                blogVO.setLikesCount(likeCountMap.get(blogVO.getId()).size());
            } else {
                blogVO.setLikesCount(0);
            }
        }

        return listBlogVO;
    }

    @Override
    public void deleteBlog(Long blogId) {
        log.debug("删除帖子业务 blogId={}",blogId);
        blogMapper.delete(blogId);
    }

    @Override
    public void saveBlog(BlogSaveParam blogSaveParam) {
        log.debug("修改帖子业务 blogSaveParam={}",blogSaveParam);
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogSaveParam,blog);
        blog.setUpdateTime(new Date());
        blogMapper.update(blog);
    }

    @Override
    public void insertBlog(BlogSaveParam blogSaveParam) {
        log.debug("保存帖子业务 blogSaveParam={}",blogSaveParam);
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogSaveParam,blog);
        blog.setCreateTime(new Date());
        blogMapper.insert(blog);
    }

    private void setUserName(BlogVO blogVO) {
        UserQuery userQuery = new UserQuery();
        userQuery.setId(blogVO.getUserId());
        List<UserVO> userVO = userMapper.selectUser(userQuery);
        for (int i =0;i<userVO.size();i++){
            blogVO.setUsername(userVO.get(i).getUsername());
        }
    }
}
